package com.cnxjs.shs4et.service.impl;

import com.cnxjs.shs4et.common.Result;
import com.cnxjs.shs4et.entity.Like;
import com.cnxjs.shs4et.repository.LikeRepository;
import com.cnxjs.shs4et.service.CommentService;
import com.cnxjs.shs4et.service.DiscussionService;
import com.cnxjs.shs4et.service.LikeService;
import com.cnxjs.shs4et.service.TopicService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeImpl implements LikeService {

    @Autowired
    private LikeRepository likeRepository;
    @Autowired
    private TopicService topicService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private DiscussionService discussionService;

    // 点赞
    @Override
    public Result add(Like like) {
        Integer likeType = like.getLikeType();
        if (likeType == 1){
            topicService.addLike(like.getLikeId());
            likeRepository.save(like);
            return Result.success("点赞成功");
        } else if (likeType == 2) {
            commentService.addLike(like.getLikeId());
            likeRepository.save(like);
            return Result.success("点赞成功");
        } else if (likeType == 3) {
            discussionService.addLike(like.getLikeId());
            likeRepository.save(like);
            return Result.success("点赞成功");
        }else {
            return Result.error("点赞失败");
        }
    }


    // 取消点赞
    @Override
    @Transactional
    public Result delete(int id) {
        likeRepository.deleteById(id);
        return Result.success("取消成功");
    }
}
